package bravo.xc.com.xc_bravo_android.operation.transaction;

import android.util.Log;

import bravo.xc.com.xc_bravo_android.beans.CommonBeans;
import bravo.xc.com.xc_bravo_android.beans.TradeResultBeans;
import bravo.xc.com.xc_bravo_android.beans.TransactionBeans;
import bravo.xc.com.xc_bravo_android.beans.TransactionResultBeans;
import bravo.xc.com.xc_bravo_android.http.HttpSingleton;
import bravo.xc.com.xc_bravo_android.http.UserOperationService;
import rx.Subscriber;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * CREATED BY YANK
 * DATE : 2017/11/3
 * MAIL : YANK.TENYOND@GMAIL.COM
 * FUNCTION :
 */

public class TransactionPresenter extends TransactionContract.TransactionPresenter {

    private UserOperationService userOperationService = HttpSingleton.getInstance().service;

    @Override
    public void doRedeem(TransactionBeans transactionBeans) {

        compositeSubscription.add(userOperationService.exchangeCode(transactionBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<TransactionResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        view.showProgress(false);
                        Log.e("@@onError1",String.valueOf(e));
                        view.showToast("网络错误，请重试");
                    }

                    @Override
                    public void onNext(TransactionResultBeans transactionResultBeans) {
                        view.showProgress(false);
                        if(transactionResultBeans.getCode() ==1){
                            view.showToast("兑换成功");
                        }else{
                            view.showToast(transactionResultBeans.getMsg());
                        }
                    }
                })
        );
    }

    @Override
    public void doGeTrade(CommonBeans commonBeans) {

        compositeSubscription.add(userOperationService.getTrade(commonBeans)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Subscriber<TradeResultBeans>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                        Log.e("@@onError2",String.valueOf(e));
                        view.showToast("网络错误，请重试");
                    }

                    @Override
                    public void onNext(TradeResultBeans tradeResultBeans) {
                        if(tradeResultBeans.getCode() == 1){
                            Log.e("@@onError",String.valueOf(tradeResultBeans.getData().get(0).getToday()));
                            view.setTradeData(tradeResultBeans.getData().get(0).getToday(),tradeResultBeans.getData().get(0).getTotal());
                        }else {
                            view.showToast("出现未知错误，请重试");
                        }
                    }
                }));
    }


}
